package com.map.application.headquarter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.jgoodies.forms.factories.FormFactory;
import com.map.database.Database;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddMap extends JFrame {
	private JTextField mapNameTextField;
	private JTextField mapTypeTextField;
	private JTextField costPriceTextField;
	private JTextField sellingPriceTextField;
	private JTextField quantityTextField;
	
	public AddMap() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddMap.class.getResource("/resources/earth_icon.png")));
		setTitle("Add Map");	
		setBounds(100, 100, 450, 350);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(55dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(14dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel mapNameLabel = new JLabel("Map Name");
		getContentPane().add(mapNameLabel, "4, 4");
		
		mapNameTextField = new JTextField();
		getContentPane().add(mapNameTextField, "8, 4, fill, default");
		mapNameTextField.setColumns(10);
		
		JLabel mapTypeLabel = new JLabel("Map Type");
		getContentPane().add(mapTypeLabel, "4, 8");
		
		mapTypeTextField = new JTextField();
		mapTypeTextField.setColumns(10);
		getContentPane().add(mapTypeTextField, "8, 8, fill, default");
		
		JLabel costPriceLabel = new JLabel("Cost Price");
		getContentPane().add(costPriceLabel, "4, 12");
		
		costPriceTextField = new JTextField();
		costPriceTextField.setColumns(10);
		getContentPane().add(costPriceTextField, "8, 12, fill, default");
		
		JLabel sellingPriceLabel = new JLabel("Selling Price");
		getContentPane().add(sellingPriceLabel, "4, 16");
		
		sellingPriceTextField = new JTextField();
		sellingPriceTextField.setColumns(10);
		getContentPane().add(sellingPriceTextField, "8, 16, fill, default");
		
		JLabel quantityLabel = new JLabel("Quantity");
		getContentPane().add(quantityLabel, "4, 20");
		
		quantityTextField = new JTextField();
		quantityTextField.setColumns(10);
		getContentPane().add(quantityTextField, "8, 20, fill, default");
		
		JButton confirmButton = new JButton("Confirm");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				
				try {
					Database db = new Database();
					db.ConnectApplicationDB();
					String insertQuery = "insert into map (mapName,mapType,costPrice,sellingPrice,quantity) values('"+mapNameTextField.getText()+"','"+mapTypeTextField.getText()+"',"+costPriceTextField.getText()+","+sellingPriceTextField.getText()+","+quantityTextField.getText()+")";
					db.exceuteQueryStatement(insertQuery);
					db.closeDB();
					HeadQuarterMainWindow.updateMapTable();
					JOptionPane.showMessageDialog(null, "Insertion Successfull !!", "alert", JOptionPane.INFORMATION_MESSAGE);
					dispose();
					
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "Insertion Failed !!", "alert", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}				
				
			}
		});
		getContentPane().add(confirmButton, "5, 24, 3, 1, center, default");
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearTextBox();
			}
		});
		getContentPane().add(cancelButton, "8, 24, center, default");
		
	}
	
	public void clearTextBox(){
		mapNameTextField.setText("");
		mapTypeTextField.setText("");
		costPriceTextField.setText("");
		sellingPriceTextField.setText("");
		quantityTextField.setText("");
	}

}
